package com.ztgf.search.api.cms;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.Md5Utils;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.search.models.dto.SearchUserDTO;
import com.ztgf.search.models.service.ISearchUserService;
import com.ztgf.search.models.vo.SearchUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/cms/searchLogin")
@Slf4j
@Api(value = "SearchLoginController", tags = "搜索引擎系统登录")
public class SearchLoginController {

    @DubboReference
    private ISearchUserService searchUserService;

    @ApiOperation(value = "退出登陆", notes = "退出登陆")
    @PostMapping(value = "/out")
    public Result<?> quiteLogin(HttpServletRequest request) {
        searchUserService.delUserSession(CommonParameter.getHeadParamsBO().getSessionId());
        return new Result<>();
    }

    @ApiOperation(value = "登陆", notes = "登陆")
    @PostMapping(value = "/in")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号", required = true, paramType = "path", dataType = "String")
            , @ApiImplicitParam(name = "password", value = "密码，md5加密", required = true, paramType = "query", dataType = "String")
    })
    public Result<?> login(String account, String password) {
        if (StrUtils.isEmpty(account) || StrUtils.isEmpty(password)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Wrapper<SearchUserDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("account", account);
        SearchUserDTO dto = searchUserService.selectOne(wrapper);
        if (dto == null) {
            return new Result<>(CodeEnum.FAIL_USER_200003);
        }
        String inputPassword = Md5Utils.MD5Encode(password.toUpperCase(), dto.getSalt());
        if (!inputPassword.equalsIgnoreCase(dto.getPassword())) {
            return new Result<>(CodeEnum.FAIL_USER_200000);
        }
        if (dto.getStatus() != 0) {
            return new Result<>(CodeEnum.FAIL_USER_200004);
        }
        String sessionId = Md5Utils.encryption(dto.getId() + "" + System.currentTimeMillis());
        //保存session进redis
        dto.setPassword(null);
        dto.setSalt(null);
        searchUserService.setUserSession(sessionId, dto);
        SearchUserVO searchUserVO = new SearchUserVO();
        searchUserVO.setId(dto.getId());
        searchUserVO.setAvatar(dto.getAvatar());
        searchUserVO.setMobile(dto.getMobile());
        searchUserVO.setEmail(dto.getEmail());
        searchUserVO.setAccount(dto.getAccount());
        searchUserVO.setName(dto.getName());
        searchUserVO.setStatus(dto.getStatus());
        searchUserVO.setType(dto.getType());
        searchUserVO.setSessionId(sessionId);
        return new Result<>(searchUserVO);
    }
}
